package com.easybuy.utils;

//import java.io.IOException;
//import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDao {
//	private static String driver="";
//	private static String url="";
//	private static String user="";
//	private static String pwd="";
	public Connection con=null;
	public PreparedStatement prestmt=null;
	public ResultSet rs=null;

	/**
	static{
		initMysqlDate();
	}
	
	public static void initMysqlDate(){
		Properties properties=null;
		try {
			properties = new Properties();
			
			InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfig.properties");
			
			properties.load(in);
			driver=properties.getProperty("driver");
			url=properties.getProperty("url");
			user=properties.getProperty("user");
			pwd=properties.getProperty("pwd");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	*/
	
	public Connection connectMysql(){
		
		try {
			//初始化上下文
			Context ctx=new InitialContext();
			//获取与逻辑名相关联的数据源对象
			DataSource ds=(DataSource) ctx.lookup("java:comp/env/easybuy");
			
			con=ds.getConnection();
			
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
}
